<?php
namespace common\components;

use yii;
use yii\base\Object;
use common\models\BaseRegion;

/**
 * @Description: 更新缓存方法集合 
 * @author: kai.gao
 * @date: 2014-12-2 下午3:50:51
 */
class UpdateCache extends Object{
	
	/**
	 * @Title: all
	 * @Description: 处理整张数据表缓存为一个key的场景 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-2 下午3:51:37
	 */
	public static function all($model, $action, $event) {
		if (in_array($action, ['insert', 'update', 'restore'])) $action = 'insert';
		switch ($action) {
			case 'insert': 
				self::insertAll($model);
			break;
			case 'delete':
			case 'rubbish':
				self::deleteAll($model);
			break;
			case 'delete_all':
				$models = $model->changeModels;
		    	foreach ($models as $model) {
		    		self::deleteAll($model);
		    	}
			break;
			case 'update_all':
				$models = $model->changeModels;
				foreach ($models as $model) {
					self::insertAll($model);
				}
			break;
		}
	}
	
	/**
	 * @Title: row
	 * @Description: 处理每一条记录生成一条缓存的场景 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-2 下午3:52:38
	 */
	public static function row($model, $action, $event) {
		if (in_array($action, ['insert', 'update', 'restore', 'rubbish'])) $action = 'insert';
		switch ($action) {
			case 'insert':
				self::insertRow($model);
				break;
			case 'delete':
				self::deleteRow($model);
				break;
			case 'delete_all':
				$models = $model->changeModels;
				foreach ($models as $model) {
					self::deleteRow($model);
				}
				break;
			case 'update_all':
				$models = $model->changeModels;
				foreach ($models as $model) {
					self::insertRow($model);
				}
				break;
		}
	}
	
	/**
	 * @Title: initAll
	 * @Description: 初始化整长表为一条缓存记录 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-2 下午3:58:05
	 */
	public static function initAll($class_name) {
		$model = new $class_name;
		$table_name = $model->getTableSchema()->name;
		$pk_name = $model->getTableSchema()->primaryKey[0];
		$query = $class_name::find()->indexBy($pk_name);
		$model->hasAttribute('is_deleted') && $query = $query->where(['is_deleted' => 0]);
		$model->hasAttribute('sort') && $query = $query->orderBy('sort');
		if ($query->count() > 3000) {
			f_f('message', '您所加载的数据量太大，可能会导致内存移除，此模型被禁用 all 模式!');
		} else {
			$data = $query->all();
			f_c($table_name, $data);
		}
	}
	
	/**
	 * @Title: initRow
	 * @Description: 初始化表中所有数据为缓存记录 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-2 下午3:58:32
	 */
	public static function initRow($class_name) {
		$model = new $class_name;
		$table_name = $model->getTableSchema()->name;
		$pk_name = $model->getTableSchema()->primaryKey[0];
		$query = $class_name::find()->indexBy($pk_name);
		$model->hasAttribute('is_deleted') && $query = $query->where(['is_deleted' => 0]);
		$limit = 3000; //防止内存溢出，每次加载3000条
		foreach ($query->each($limit) as $data) {
			f_c($table_name.'_'.$data->$pk_name, $data);
		}
	}
	
	/**
	 * @Title: insertRow
	 * @Description: 新增 修改缓存中的数据 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-2 下午3:53:46
	 */
	private static function insertRow($model) {
		$table_name = $model->getTableSchema()->name;
		$pk = $model->getPrimaryKey();
		$key = $table_name.'_'.$pk;
		f_c($key, $model);
	}
	
	/**
	 * @Title: deleteRow
	 * @Description: 删除缓存中的数据 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-2 下午3:54:37
	 */
	private static function deleteRow($model) {
		$table_name = $model->getTableSchema()->name;
    	$pk = $model->getPrimaryKey();
    	$key = $table_name.'_'.$pk;
    	f_c($key, false);
	}
	
	/**
	 * @Title: insertAll
	 * @Description: 单条集合缓存插入、修改数据 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-2 下午3:54:54
	 */
	private static function insertAll($model) {
		$table_name = $model->getTableSchema()->name;

		$pk = $model->getPrimaryKey();
		if ($model->hasAttribute('img_id')) $attributes['img'] = $model->img;
		$data = f_c($table_name);
		$data[$pk] = $model;
		f_c($table_name, $data);
	}
	
	/**
	 * @Title: deleteAll
	 * @Description: 单条集合缓存删除数据 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-2 下午3:55:31
	 */
	private static function deleteAll($model) {
		$table_name = $model->getTableSchema()->name;
		$pk = $model->getPrimaryKey();
		$data = f_c($table_name);
		unset($data[$pk]);
		f_c($table_name, $data);
	}
	
	/**
	 * 
	 * @Title: initRegion
	 * @Description: 初始化区域信息 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-10下午11:13:25
	 */
	public static function initRegion($class_name) {
		$db = \Yii::$app->db;
		$mem = \Yii::$app->rcache;
		for ($i = 0; $i <= 2; $i++) {
			$sql = "select region_name,region_id from {{%base_region}} as t1 where t1.region_type = $i";
			$regions = $db->createCommand($sql)->queryAll();
			foreach ($regions as $pval) {
				$key = 'region_'.$pval['region_id'];
				$data = BaseRegion::getRegion($pval['region_id']);
				$mem->set($key, $data);
			}
		}
	}
	
	/**
	 * @Title: initSiteArea
	 * @Description: 初始化区县对应的站点 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2015-1-4下午3:38:30
	 */
	public static function initSiteArea($class_name) {
		$db = Yii::$app->db;
		$mem = Yii::$app->rcache;
		$sql = "select region_name,region_id from {{%base_region}} as t1 where t1.region_type = 3";
		$regions = $db->createCommand($sql)->queryAll();
		foreach ($regions as $val) {
			$key = 'site_area_'.$val['region_id'];
			$data = BaseRegion::getSite($val['region_id']);
			$mem->set($key, $data);
		}
	}
}

?>